<?php
/**
 *@file commentban.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-7-15 下午5:33:29
 *@brief  
 */
namespace haibao\comment\business;

use haibao\frame\data\query\Condition;
use haibao\comment\data\CommentBan as CommentBanData;
use haibao\cms\business\BusinessException;

class CommentBan extends \haibao\cms\business\Base{
    
    const COMMENT_BAN_YES_REASON = '非法'; 
    const COMMENT_BAN_NO_REASON = '合法用户'; 
	public static function addCommentBan($commentBanModel){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD);
		if(!$commentBanModel){
			throw new BusinessException('冻结信息不能为空');
		}
		$commentBan = new \haibao\comment\data\CommentBan();
		$commentBan->add($commentBanModel);
		self::addCommentBanLog($commentBanModel->Id,self::COMMENT_BAN_YES_REASON);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD,$commentBanModel->toArray(true,true));
	}
	
	public static function checkCommentBan($info,$type){
	    $commentBanData = new CommentBanData();
	    return $commentBanData->getBan($info,$type);
	}
	
	public static function checkBanOrNot($info,$banType){
		$commentBanFilter = new \haibao\comment\model\filter\CommentBan();
		$commentBanFilter->where($commentBanFilter::CONDITION_FIELD_NAME_BANSTATUS, Condition::CONDITION_EQUAL, \haibao\comment\model\data\CommentBan::BAN_STATUS_NO);
		if($banType == \haibao\comment\model\data\CommentBan::BAN_TYPE_USER){
			$commentBanFilter->where($commentBanFilter::CONDITION_FIELD_NAME_USERID, Condition::CONDITION_EQUAL, $info);
		}else{
			$commentBanFilter->where($commentBanFilter::CONDITION_FIELD_NAME_IP, Condition::CONDITION_EQUAL, $info);
		}
		$commentBanData = new CommentBanData();
		return $commentBanData->getOne($commentBanFilter);
	}
	
	public static function addCommentBanLog($id,$reason){
		if(!$id){
			throw new BusinessException('记录错误，冻结Id不能为空');
		}
		$commentBanLogData = new \haibao\comment\model\data\CommentBanLog();
		$commentBanLogData->Reason = $reason;
		$commentBanLogData->BanId = $id;
		$commentBanLogData->CreateBy = \haibao\cms\business\Base::getCurrentUser()->Id;
		$commentBanLogData->CreateTime = new \DateTime();
		$commentBanLog = new \haibao\comment\data\CommentBanLog();
		$commentBanLog->add($commentBanLogData);
	}

	public static function getSeal($editInfo){
	    $commentBanData = new CommentBanData();
	    return $commentBanData->getSeal($editInfo);
	}
	
	public static function updateCommentBanYes($id,$arrSet){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD);
		$commentBanData = new CommentBanData();
		$commentBanData->updateById($id,$arrSet);
		self::addCommentBanLogs($id,self::COMMENT_BAN_NO_REASON);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD, array_merge(array('Id'=>$id),$arrSet));
	}
	public static function updateCommentBanNo($id,$arrSet){
	    self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD);
	    $commentBanData = new CommentBanData();
	    $commentBanData->updateById($id,$arrSet);
	    self::addCommentBanLog($id,self::COMMENT_BAN_YES_REASON);
	    self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENT_BAN_ADD, array_merge(array('Id'=>$id),$arrSet));
	}

	public static function addCommentBanLogs($id,$reson){
	    if(!$id){
	        throw new BusinessException('记录错误，冻结Id不能为空');
	    }
	    $commentBanLogData = new \haibao\comment\model\data\CommentBanLog();
	    $commentBanLogData->Reason = $reson;
	    $commentBanLogData->BanId = $id;
	    $commentBanLogData->CreateBy = \haibao\cms\business\Base::getCurrentUser()->Id;
	    $commentBanLogData->CreateTime = new \DateTime();
	    $commentBanLog = new \haibao\comment\data\CommentBanLog();
	    $commentBanLog->add($commentBanLogData);
	}
	

	public static function checkUserIsBan($userId,$ip){
		
		if(!isset($userId)){
			throw new BusinessException('用户ID为空');
		}

		if(!isset($ip)){
			throw new BusinessException('IP为空');
		}
		$commentBanData = new CommentBanData();
		return $commentBanData->checkUserIsBan($userId,$ip);
	}
	
}